package com.linxz.wandnroid.appui;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;

import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.linxz.evens.ARouterURLConfig;
import com.linxz.framework.base.BaseMVPActivity;
import com.linxz.ui.pager_indictor.ColorFlipPagerTitleView;
import com.linxz.ui.widgets.SmartLayout;
import com.linxz.wandnroid.R;
import com.linxz.wandnroid.adapter.GankFragAdapter;
import com.linxz.wandnroid.appui.view.GankHomeView;
import com.linxz.wandnroid.pojo.GankResultBean;
import com.linxz.wandnroid.presenter.GankHomePresenter;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.List;

/**
 * @author Linxz
 * 创建日期：2020年02月28日 11:41
 * version：v4.5.4
 * 描述：
 */
@Route(path = ARouterURLConfig.WAN_GANK_HOME)
public class GankHomeActivity extends BaseMVPActivity<GankHomePresenter> implements GankHomeView {

    private SmartLayout layoutSmart;
    private ViewPager viewPager;
    private List<String> category;

    @Override
    protected GankHomePresenter createPresenter() {
        return new GankHomePresenter(this,lifecycleSubject);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wandroid_act_gank_home);
        initUI();
        setListener();
        initData();
    }

    @Override
    protected void initUI() {
        layoutSmart=findViewById(R.id.layoutSmart);
        viewPager=findViewById(R.id.viewPager);
        layoutSmart.showLoadingView();
    }

    @Override
    protected void initData() {
        mvpPresenter.getGank();
    }

    private void initMagicIndicator() {
        MagicIndicator magicIndicator = findViewById(R.id.magicIndicator);
        magicIndicator.setBackgroundColor(Color.parseColor("#ffffff"));
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setScrollPivotX(0.65f);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return category == null ? 0 : category.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorFlipPagerTitleView(context);
                simplePagerTitleView.setText(category.get(index));
                simplePagerTitleView.setTextSize(16);
                simplePagerTitleView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                simplePagerTitleView.setNormalColor(Color.parseColor("#9e9e9e"));
                simplePagerTitleView.setSelectedColor(Color.parseColor("#00c853"));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setLineHeight(UIUtil.dip2px(context, 6));
                indicator.setLineWidth(UIUtil.dip2px(context, 10));
                indicator.setRoundRadius(UIUtil.dip2px(context, 3));
                indicator.setStartInterpolator(new AccelerateInterpolator());
                indicator.setEndInterpolator(new DecelerateInterpolator(2.0f));
                indicator.setColors(Color.parseColor("#00c853"));
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, viewPager);
    }

    @Override
    public void onLoadSuccess(List<String> category, GankResultBean data) {
        this.category=category;
        GankFragAdapter adapter = new GankFragAdapter(getSupportFragmentManager(), category, data);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(0);
        initMagicIndicator();
        layoutSmart.showNormal();
    }

    @Override
    public void onLoadError(String message) {
        layoutSmart.showErrorView();
    }

    @Override
    public void onDataEmpty() {
        layoutSmart.showEmptyView();
    }
}
